package de.dlrg.wms.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import org.springframework.stereotype.Component;

import de.dlrg.wms.model.NullClasses.RunNull;

@Entity
public class CompetitionStatus {
	public enum RunState {
		CANCELLED, FINISHED, NO_RUNS_AVAILABLE, NOT_STARTED, RUNNING, WAITING_FOR_READY, WAITING_FOR_START,
	};
	
	@OneToOne
	private Run currentRun = null;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private boolean paused = false;

	private RunState runState = RunState.NO_RUNS_AVAILABLE;

	@OneToOne
	private SwimmerTrackRun waitingSwimmerTrackRun;

	public void FINISHED() {
		this.runState = RunState.FINISHED;
	}

	public Run getCurrentRun() {
		return currentRun;
	}

	public Long getId() {
		return id;
	}

	public void getNextWaitingSwimmerTrackRun() {
		for(SwimmerTrackRun swimmerTrackRun : this.getCurrentRun().getSwimmerTrackRun()) {
			if(!swimmerTrackRun.isReady()) {
				this.waitingSwimmerTrackRun = swimmerTrackRun;
				return;
			}
		}
		this.waitingSwimmerTrackRun = null;
	}

	public RunState getRunState() {
		return runState;
	}

	public SwimmerTrackRun getWaitingSwimmerTrackRun() {
		return waitingSwimmerTrackRun;
	}

	public boolean isPaused() {
		return paused;
	}

	public void NO_RUNS_AVAILABLE() {
		this.runState = RunState.NO_RUNS_AVAILABLE;
	}

	public void NOT_STARTED() {
		this.runState = RunState.NOT_STARTED;
	}

	public void RUNNING() {
		this.runState = RunState.RUNNING;
	}

	public CompetitionStatus setCurrentRun(Run currentRun) {
		this.currentRun = currentRun;
		return this;
	}

	public CompetitionStatus setId(Long id) {
		this.id = id;
		return this;
	}

	public void setPaused(boolean paused) {
		this.paused = paused;
	}

	public void setRunState(RunState runState) {
		this.runState = runState;
	}

	public void setWaitingSwimmerTrackRun(SwimmerTrackRun waitingSwimmerTrackRun) {
		this.waitingSwimmerTrackRun = waitingSwimmerTrackRun;
	}

	public void WAITING_FOR_READY() {
		this.runState = RunState.WAITING_FOR_READY;
	}

	public void WAITING_FOR_START() {
		this.runState = RunState.WAITING_FOR_START;
	}
}
